package com.example.mybatisplusdemo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplusdemo.dto.UserLoginDTO;
import com.example.mybatisplusdemo.dto.UserPageDTO;
import com.example.mybatisplusdemo.entity.Address;
import com.example.mybatisplusdemo.entity.User;
import com.example.mybatisplusdemo.exceptionHandle.BusinessException;
import com.example.mybatisplusdemo.exceptionHandle.LoginException;
import com.example.mybatisplusdemo.mapper.AddressMapper;
import com.example.mybatisplusdemo.mapper.UserMapper;
import com.example.mybatisplusdemo.result.PageResult;
import com.example.mybatisplusdemo.service.UserService;
import com.example.mybatisplusdemo.vo.UserDTO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AddressMapper addressMapper;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        LambdaQueryWrapper< User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, userLoginDTO.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        // 判断用户是否存在
        if(user == null){
            throw LoginException.userNotFound();
        }

        // 密码校验
        if(!userLoginDTO.getPassword().equals(user.getPassword())){
            throw LoginException.passwordError();
        }
        return user;
    }

    /**
     * 获取用户列表
     * @param userPageDTO
     * @return
     */
    @Override
    public PageResult<User> getPage(UserPageDTO userPageDTO) {
        Page<User> page = new Page<>(userPageDTO.getPageNo(), userPageDTO.getPageSize());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getUsername, userPageDTO.getUsername());
        Page<User> pageResult = this.page(page, queryWrapper);

        PageResult<User> result = new PageResult<>();
        result.setTotal(pageResult.getTotal());
        result.setList(pageResult.getRecords());
        return result;
    }

    /**
     * 获取用户信息(包括地址)
     * @return
     */
    @Override
    public List<UserDTO> getUserInfo() {
//        //查询所有用户信息
//        List<User> userList = userMapper.selectList(null);
//        //给每个用户添加地址信息
//        for (User user : userList) {
//            Long addressId = user.getAddressId();
//            Address address = addressMapper.selectById(addressId);
//            user.setAddress(address);
//        }
//        List<UserDTO> userDTOList = BeanUtil.copyToList(userList, UserDTO.class);

        //获取所有的地址信息以key:id,value:address
        Map<Long, Address> addressMap = addressMapper.selectList(null).stream().collect(Collectors.toMap(Address::getId, address -> address));
        //查询所有用户信息
        List<User> userList = userMapper.selectList(null);
        //给每个用户添加地址信息
        List<UserDTO> userDTOList = userList.stream().map(user -> {
            UserDTO userDTO = new UserDTO();
            BeanUtil.copyProperties(user, userDTO);
            Long addressId = user.getAddressId();
            Address address = addressMap.get(addressId);
            userDTO.setAddress(address);
            return userDTO;
        }).collect(Collectors.toList());

        return userDTOList;
    }
}
